/*** Trigger Name: QueryTriggers 
 * Author: Accenture
 * Date: 13-July-2011
 * Requirement/Project Name: iFulfill
 * Requirement Description: 
 **/
trigger QueryTriggers on Queries__c (before insert, before update, after update) {

    // Check if triggerd is fired  before or after
    if(trigger.isBefore)
    {
    
        list<Queries__c> updateTemplateItems = new list<Queries__c>();  
        List<Queries__c> expirationDateQueryList= new List<Queries__c>(); 
   
        if(trigger.isInsert)
        {
  
          // update Query SLA based on FI region Type      
          iFulfillQueryHandler.updateQuerySla(Trigger.New);
                     
          for(Queries__c Q:Trigger.New){

                Q.Query_Open_Date__c = system.now();                               
                if(Q.Query_Reason__c !=null){
                    updateTemplateItems.add(Q);
                }                                           
            }
            iFulfillQueryHandler.updateAutoFields(Trigger.New);
        }
        else if(trigger.isUpdate){
            
            for(Queries__c Q:Trigger.New){
                String qOwnerId=string.valueof(Q.QR_Ownerid__c);  
                
                if(qOwnerId.startswith(IfullfillTextConstant.OWNERVALUE)){
                Q.Query_Request_Owner__c = Q.QR_Ownerid__c;
                }
                // Update email template if query code is changed 
                if(Q.Query_Reason__c !=Trigger.oldMap.get(Q.id).Query_Reason__c)
                {
                    updateTemplateItems.add(Q); }
                if((Q.External_Establish_Date__c != Trigger.oldMap.get(Q.id).External_Establish_Date__c)
                    || (Q.Requested_Date__c != Trigger.oldMap.get(Q.id).Requested_Date__c)){
                    expirationDateQueryList.add(Q);                    
                 }     
            }   
             if(expirationDateQueryList.size()>0){    
                 iFulfillQueryHandler.populateExpirationDate(expirationDateQueryList);     
             } 
        }      
        // Update email template id based on the  query code.
        if(updateTemplateItems.size()>0) 
            iFulfillQueryHandler.updateEmailTemplate(updateTemplateItems);
    }
    else{
        
        Set<Id> ReqIds = new Set<Id>();        
        list<Queries__c> updateStatusReq = new list<Queries__c>();                    
        for(Queries__c Q:Trigger.New){
        
            // Check If query is resolved/ Unresolved.
            if(Q.Query_Status__c != Trigger.oldMap.get(Q.id).Query_Status__c && (Q.Query_Status__c == 'Resolved' || Q.Query_Status__c == 'Unresolved' )){
            
                    updateStatusReq.add(Q);
                    ReqIds.add(Q.Query_Request_ID__c);  
            }
       }
       // Update the query request status if all the query of the query request are resolved.
       if(updateStatusReq.size()>0) 
          iFulfillQueryHandler.updateReqStatus(updateStatusReq,ReqIds);            
        }
    
}